package org.rscdaemon.server.packetbuilder.loginserver;

import org.rscdaemon.server.packetbuilder.LSPacketBuilder;
import org.rscdaemon.server.model.Player;
import org.rscdaemon.server.model.InvItem;
import org.rscdaemon.server.model.Inventory;
import org.rscdaemon.server.model.Bank;
import org.rscdaemon.server.model.PlayerAppearance;
import org.rscdaemon.server.net.LSPacket;
import org.rscdaemon.server.util.DataConversions;

public class SavePacketBuilder {
	/**
	 * Player to save
	 */
	private Player player;
	
	/**
	 * Sets the player to save
	 */
	public void setPlayer(Player player) {
		this.player = player;
	}

	public LSPacket getPacket() {
		LSPacketBuilder packet = new LSPacketBuilder();
		packet.setID(20);
		packet.addLong(player.getUsernameHash());
		packet.addInt(player.getOwner());
		
		packet.addLong(player.getLastLogin() == 0L && player.isChangingAppearance() ? 0 : player.getCurrentLogin());
		packet.addLong(DataConversions.IPToLong(player.getCurrentIP()));
		packet.addShort(player.getCombatLevel());
		packet.addShort(player.getSkillTotal());
		packet.addShort(player.getX());
		packet.addShort(player.getY());
		packet.addShort(player.getFatigue());
		packet.addShort(player.getMuted());
		
		PlayerAppearance a = player.getPlayerAppearance();
		packet.addByte((byte)a.getHairColour());
		packet.addByte((byte)a.getTopColour());
		packet.addByte((byte)a.getTrouserColour());
		packet.addByte((byte)a.getSkinColour());
		packet.addByte((byte)a.getSprite(0));
		packet.addByte((byte)a.getSprite(1));
		
		packet.addByte((byte)(player.isMale() ? 1 : 0));
		packet.addLong(player.getSkullTime());
		packet.addByte((byte)player.getCombatStyle());
		
		for(int i = 0;i < 18;i++) {
			packet.addLong(player.getExp(i));
			packet.addShort(player.getCurStat(i));
		}
		
		Inventory inv = player.getInventory();
		packet.addShort(inv.size());
		for(InvItem i : inv.getItems()) {
			packet.addShort(i.getID());
			packet.addInt(i.getAmount());
			packet.addByte((byte)(i.isWielded() ? 1 : 0));
		}
		
		Bank bnk = player.getBank();
		packet.addShort(bnk.size());
		for(InvItem i : bnk.getItems()) {
			packet.addShort(i.getID());
			packet.addInt(i.getAmount());
		}
		
		return packet.toPacket();
	}
	
}

